package com.xrz.sxm.aj.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextPaint;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.entity.XueweiEntity;
import com.xrz.sxm.aj.entity_tab.DBHelps;
import com.xrz.sxm.aj.entity_tab.Jlb;
import com.xrz.sxm.aj.listener.CloseAnimationListener;
import com.xrz.sxm.aj.utils.DisplayUtil;
import com.xrz.sxm.aj.utils.ScreenUtil;
import com.xrz.sxm.aj.utils.Utils;
import com.xrz.sxm.aj.view.ViewArea;

/**
 * @author 辛汝州 2014-12-17
 */
public class Act_XueWei extends BaseAct implements OnClickListener, Runnable {

	private static final String CLOSE = "close";
	private static final String OPEN = "open";

	@ViewInject(R.id.test)
	LinearLayout layout;

	@ViewInject(R.id.split)
	private ImageView split;

	@ViewInject(R.id.sv_top)
	private ScrollView m_ScrollView;

	// //////////////////////////////////////取穴描述
	@ViewInject(R.id.rtly_quxue_click)
	private RelativeLayout rtly_quxue_click;
	@ViewInject(R.id.quxue_state)
	private TextView quxue_state;
	@ViewInject(R.id.tv_quxue)
	private TextView tv_quxue;
	// ///////////////////////////////////////////主治病症
	@ViewInject(R.id.rtly_zhuzhi_click)
	private RelativeLayout rtly_zhuzhi_click;
	@ViewInject(R.id.zhuzhi_state)
	private TextView zhuzhi_state;
	@ViewInject(R.id.tv_zhuzhi)
	private TextView tv_zhuzhi;
	// //////////////////////////////////////////艾灸与临床应用
	@ViewInject(R.id.rtly_yingyong_click)
	private RelativeLayout rtly_yingyong_click;
	@ViewInject(R.id.yingyong_state)
	private TextView yingyong_state;
	@ViewInject(R.id.tv_yingyong)
	private TextView tv_yingyong;
	// ///////////////////////////////所属经络
	@ViewInject(R.id.llyt_shuyu_click)
	private RelativeLayout llyt_shuyu_click;
	@ViewInject(R.id.tv_shuyu_jingmai)
	private TextView tv_shuyu_jingmai;

	private Animation m_Animation_open, m_Animation_close;

	@ViewInject(R.id.view)
	private LinearLayout m_ZoomLayout;

	Handler handler = new Handler();

	private int h1, h2, h3;

	private boolean IsQuexueGone = false, IsZhuzhiGone = true,
			IsYingyongGone = true;

	/** 传过来的穴位名 */
	private XueweiEntity m_XueweiEntity;
	private Jlb m_JingluoEntity;

	@Override
	protected int setView() {
		return R.layout.layout_xuwwei;
	}

	private DBHelps m_DbHelps;

	@Override
	protected void init() {
		m_DbHelps = new DBHelps(ctx);
		m_JingluoEntity = m_DbHelps.getJingluoByDaima(m_XueweiEntity.jldm);
		if (m_JingluoEntity != null)
			tv_shuyu_jingmai.setText("所属经脉---" + m_JingluoEntity.jlmc);
		m_Animation_open = AnimationUtils.loadAnimation(ctx, R.anim.scale_open);
		m_Animation_close = AnimationUtils.loadAnimation(ctx,
				R.anim.scales_close);
		rtly_quxue_click.setOnClickListener(this);
		rtly_yingyong_click.setOnClickListener(this);
		rtly_zhuzhi_click.setOnClickListener(this);
		llyt_shuyu_click.setOnClickListener(this);

		tv_quxue.setText(Utils.replaceChar(m_XueweiEntity.qxms));
		tv_zhuzhi.setText(Utils.replaceChar(m_XueweiEntity.zzbz));
		tv_yingyong.setText(Utils.replaceChar(m_XueweiEntity.lcyy));

		TextPaint paint = tv_quxue.getPaint();
		float len = paint.measureText(tv_quxue.getText().toString());
		h1 = (((int) len / ScreenUtil.getInstant().getDisWidth(ctx)) + 1)
				* DisplayUtil.sp2px(ctx, 16);

		paint = tv_zhuzhi.getPaint();
		len = paint.measureText(tv_quxue.getText().toString());
		h2 = (((int) len / ScreenUtil.getInstant().getDisWidth(ctx)) + 1)
				* DisplayUtil.sp2px(ctx, 16);

		paint = tv_quxue.getPaint();
		len = paint.measureText(tv_yingyong.getText().toString());
		h3 = (((int) len / ScreenUtil.getInstant().getDisWidth(ctx)) + 1)
				* DisplayUtil.sp2px(ctx, 16);
		// handler.postDelayed(this, 1000);
		Log.e(TAG, "h1=" + h1 + "   h2=" + h2 + "   h3=" + h3);

		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		rtly_quxue_click.measure(w, h);
		int height = rtly_quxue_click.getMeasuredHeight();
		changeHight(height);
		String picpath = m_XueweiEntity.tmlj;
		Log.e("picpath......",picpath);
		int res = R.mipmap.ic_launcher;
		int a = Utils.getImageByPicName(picpath);

		if (a != 0) {
			res = a;
		}
		ViewArea viewArea = new ViewArea(ctx, res);
		LinearLayout.LayoutParams parm = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		m_ZoomLayout.addView(viewArea, parm);
	}

	@Override
	protected String setTopTitle() {
		m_XueweiEntity = (XueweiEntity) getIntent()
				.getSerializableExtra("data");
		Log.e(TAG, m_XueweiEntity.toString());
		return m_XueweiEntity.xwmc;
	}

	/**
	 * 控制切换
	 *
	 * @param view
	 *            点击的View
	 * @param ContentView
	 *            显示内容的View
	 * @param stateView
	 *            加减号的TextView
	 */
	private void changeState(View view, View ContentView, TextView stateView) {
		String tag = (String) view.getTag();
		if (tag.equals(CLOSE)) {
			ContentView.setVisibility(View.VISIBLE);
			ContentView.startAnimation(m_Animation_open);
			view.setTag(OPEN);
			stateView.setText("-");
		} else {
			ContentView.startAnimation(m_Animation_close);
			m_Animation_close.setAnimationListener(new CloseAnimationListener(
					ContentView));
			view.setTag(CLOSE);
			stateView.setText("+");
		}
		changeHight(0);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
			case R.id.rtly_quxue_click:
				IsQuexueGone = !IsQuexueGone;
				changeState(arg0, tv_quxue, quxue_state);
				break;
			case R.id.rtly_yingyong_click:
				IsYingyongGone = !IsYingyongGone;
				changeState(arg0, tv_yingyong, yingyong_state);
				break;
			case R.id.rtly_zhuzhi_click:
				IsZhuzhiGone = !IsZhuzhiGone;
				changeState(arg0, tv_zhuzhi, zhuzhi_state);
				break;
			case R.id.llyt_shuyu_click:
				startActivity(new Intent(this, Act_JingMai.class).putExtra("data",
						m_JingluoEntity));
				break;
		}
		// handler.postDelayed(this, 600);
	}

	/**
	 * 这个方法是测试的方法，可能取值会不准确 就是onCreate中h1,h2,h3算的值有偏差 如果客户反映有问题
	 * 还是使用handler.postDelayed(this, 600);这个方法 但是有延迟
	 */
	private void changeHight(int hight) {
		int h11 = IsQuexueGone ? 0 : h1;
		int h21 = IsZhuzhiGone ? 0 : h2;
		int h31 = IsYingyongGone ? 0 : h3;
		int allHight = (hight == 0 ? rtly_quxue_click.getHeight() * 4
				: hight * 4) + h11 + h21 + h31;
		LayoutParams localLayoutParams1 = (LayoutParams) this.m_ScrollView
				.getLayoutParams();
		localLayoutParams1.height = Math.min(ScreenUtil.getInstant()
				.getDisHight(ctx) / 2, allHight);
		Log.e(TAG, "allHight=" + allHight);
		m_ScrollView.setLayoutParams(localLayoutParams1);
	}

	@Override
	public void run() {
		// int h1 = tv_quxue.getVisibility() == View.GONE ? 0 : tv_quxue
		// .getHeight();
		// int h2 = tv_yingyong.getVisibility() == View.GONE ? 0 : tv_yingyong
		// .getHeight();
		// int h3 = tv_zhuzhi.getVisibility() == View.GONE ? 0 : tv_zhuzhi
		// .getHeight();
		// Log.e(TAG, "h1=" + h1 + "  h2=" + h2 + "   h3=" + h3);
		// int allHight = rtly_quxue_click.getHeight() * 4 + h1 + h2 + h3;
		// Log.e(TAG,
		// "rtly_quxue_click.getHeight()=" + rtly_quxue_click.getHeight());
		// Log.e(TAG, "allHight=" + allHight);
		// LayoutParams localLayoutParams1 = (LayoutParams) this.m_ScrollView
		// .getLayoutParams();
		// localLayoutParams1.height = Math.min(-24
		// + ScreenUtil.getInstant().getDisHight(ctx) / 2, allHight);
		// Log.e(TAG, "localLayoutParams1.height=" + localLayoutParams1.height);
		// m_ScrollView.setLayoutParams(localLayoutParams1);
	}

}
